#include "gdb_queue.h"


void gdb_queue_init(GdbQueue * pQueue)
{
	pQueue->start = 0;
	pQueue->end = 1;
}

char gdb_dequeue (GdbQueue * pQueue)
{
	char toRet;
	int start  = pQueue->start + 1;
	if (start == GDB_QUEUE_SIZE)
		start = 0;
	while (start == pQueue->end);
	toRet = pQueue->data[start];
	pQueue->start = start;
	return toRet;
}

void gdb_enqueue(GdbQueue * pQueue, char c)
{
	int end = pQueue->end ;
	if (end == pQueue->start)
	{
		//printf("GDB queue overflow - please increase the size of the queue");
		return;
	}
	pQueue->data[end] = c;
	end++;
	if (end == GDB_QUEUE_SIZE)
		end = 0;
	pQueue->end = end ;
}

#ifdef GQ_MAIN

void print_q(GdbQueue *q);
int main()
{
	GdbQueue q;
	gdb_queue_init(&q);
	gdb_enqueue(&q, 'a');
	gdb_enqueue(&q, 'b');
	gdb_enqueue(&q, 'c');
	gdb_dequeue(&q);
	gdb_dequeue(&q);
	print_q(&q);
	gdb_enqueue(&q, 'd');
	print_q(&q);
	gdb_dequeue(&q);
	print_q(&q);
	gdb_dequeue(&q);
	print_q(&q);
	gdb_dequeue(&q);


	return 0;
}
void print_q(GdbQueue *q)
{
	printf("printing queue contents...\n");
	int index = q->start;
	int end;
	if (q->end == 0)
		end = GDB_QUEUE_SIZE - 1;
	else
		end = q->end - 1;
	//printf("end is %d", end);
	//return ;
	while (index != end )
	{
		index++;
		if (index == GDB_QUEUE_SIZE)
			index = 0;
		printf("i: %d val: %c....\n", index, q->data[index]);
	}
	printf("\n");
}

#endif
